from math import sqrt

class HomVec3(object):
  
  def __init__(self,x,y,z,w):
      self.w = w  
      self.x = x 
      self.y = y
      self.z = z 

  def __repr__(self):
    return 'HomV(%s, %s, %s, %s)' % (self.x, self.y, self.z, self.w) 

  def isPoint(self):
    return self.w > 0
      
  def isVector(self):
    return self.w == 0
    
  def __add__(self,vec):
    return HomVec3(self.x + vec.x, self.y + vec.y, self.z + vec.z,self.w + vec.w)

  def __sub__(self,vec):
    if not(self.isVector() and vec.isPoint()):
      return HomVec3(self.x - vec.x, self.y - vec.y, self.z - vec.z,self.w - vec.w)

  def length(self):
    if self.isVector():
      return sqrt(self.x**2 + self.y**2 + self.z**2)

  def __div__(self,b):
    return HomVec3(self.x / b, self.y / b, self.z / b,self.w)

  def normalized(self):
    return self / self.length()

  def __mul__(self,b):
    
    if type(b) != HomVec3:
      #TODO: Multiplikation mit Skalar
      pass
    else:
      return ((self.x * b.x) + (self.y * b.y) + (self.z * b.z))
  
  def __mod__(self, vec):
    return HomVec3(self.y * vec.z - self.z * vec.y, self.z * vec.x - self.x * vec.z, self.x * vec.y - self.y * vec.x, 0)

"""
if __name__ == "__main__":
  
  p1, p2, p3 = HomVec3(2, 2, 0, 1), HomVec3(4, 5, 0, 1), HomVec3(6, 3, 0, 1)
  print p1, p2, p3
  print "Flaecheninhalt:"
  print "1. Variante: ", ((p2-p1)%(p3-p1)).length()/2
  print "2. Variante: ", (p1%p2+p2%p3+p3%p1).length()/2
"""
    
